<template>
    <div class="addCompany-body">
        <div class="addCompany-content">
            <div class="form-inline">
                <div class="form-group">
                    <label for="Input1">公司 LOGO：</label>
                    <img :src="logoImg" alt="logo" class="img-rounded" @click="AsyncUpload()">
                </div>
                <div class="form-group">
                    <label for="Input1">注册号：</label>
                    <input type="text" class="form-control" id="Input1" v-model="company.regcode">
                </div>
                <div class="form-group">
                    <label for="Input2">公司名：</label>
                    <input type="email" class="form-control" id="Input2" v-model="company.epname">
                </div>
                <div class="form-group">
                    <label for="Input3">地址：</label>
                    <input type="text" class="form-control" id="Input3" v-model="company.address">
                </div>
                <div class="form-group">
                    <label for="Input4">经营范围：</label>
                    <input type="email" class="form-control" id="Input4" v-model="company.runscope">
                </div>
                <div class="form-group">
                    <label for="Input5">手机号：</label>
                    <input type="text" class="form-control" id="Input5" v-model="company.mobiletele">
                </div>
                <div class="form-group">
                    <label for="Input6">座机：</label>
                    <input type="email" class="form-control" id="Input6" v-model="company.cellphone">
                </div>
                <div class="form-group">
                    <label for="Input7">成立时间：</label>
                    <input type="text" class="form-control" id="Input7" v-model="company.regtime">
                </div>
                <div class="form-group">
                    <label for="Input8">法律代表人：</label>
                    <input type="email" class="form-control" id="Input8" v-model="company.legalperson">
                </div>
            </div>
            <div class="form-group form-editor" :style="formEditorWidHei">
                <label>公司简介：</label>
                <editor></editor>
            </div>
            <div class="form-submit">
                <button class="btn btn-primary btn-lg btn-block" @click="toSubmit()">提交信息</button>
            </div>
        </div>
    </div>
</template>

<script>
    var AsyncUploadFile = require('../util/AsyncUpload');
    module.exports = {
        data: function () {
            return {
                formEditorWidHei: {
                    width: ($(window).width() - 550) + 'px'
                },
                logoImg: require('../assets/images/plus.png'),
                company: {
                    regcode: '',
                    epname: '',
                    address: '',
                    runscope: '',
                    mobiletele: '',
                    cellphone: '',
                    regtime: '',
                    legalperson: '',
                    editorHtml: ''
                }
            }
        },
        ready: function () {
            var _this = this;
            var CompanyID = this.$route.params.id;
            // 如果 CompanyID 存在则执行
            if ( CompanyID ) {
                $.post('/Enterprise/FindById', {"id": CompanyID}).then(function (data) {
                    _this.logoImg = data.content.logo;
                    _this.company.epname = data.content.epname;
                    _this.company.regcode = data.content.regcode;
                    _this.company.address = data.content.address;
                    _this.company.runscope = data.content.runscope;
                    _this.company.mobiletele = data.content.mobiletele;
                    _this.company.cellphone = data.content.cellphone;
                    _this.company.regtime = data.content.regtime;
                    _this.company.legalperson = data.content.legalperson;
                    _this.company.editorHtml = data.content.intruduction;
                    // 派发信息给子组件
                    _this.$broadcast('editor', _this.company.editorHtml);
                });
            }
        },
        methods: {
            AsyncUpload: function () {
                var _this = this;
                AsyncUploadFile.upload({
                    //上传文件接收地址
                    uploadUrl: "/api/planb/logoUpload",
                    //选择文件后，发送文件前自定义事件
                    //file为上传的文件信息，可在此处做文件检测、初始化进度条等动作
                    beforeSend: function (file) {

                    },
                    //文件上传完成后回调函数
                    //res为文件上传信息
                    callback: function (res) {
                        var data = JSON.parse(res);
                        if (data.status == 'error') {
                            alert(data.msg);
                        } else {
                            _this.logoImg = data.msg;
                        }
                    },
                    //返回上传过程中包括上传进度的相关信息
                    //详细请看res,可在此加入进度条相关代码
                    uploading: function (res) {

                    }
                });
            },
            toSubmit: function () {
                var _this = this;
                var CompanyID = _this.$route.params.id;
                if (CompanyID) {
                    $.post('/Enterprise/UpdateEnterprise', {
                        "id": CompanyID,
                        "regcode": _this.company.regcode,
                        "epname": _this.company.epname,
                        "address": _this.company.address,
                        "runscope": _this.company.runscope,
                        "mobiletele": _this.company.mobiletele,
                        "cellphone": _this.company.cellphone,
                        "regtime": _this.company.regtime,
                        "legalperson": _this.company.legalperson,
                        "intruduction": _this.company.editorHtml
                    }, function (data) {
                        alert(data.msg);
                        if( data.status == 'success' ){
                            _this.$router.go("/index/companyInfo/"+CompanyID);
                        }
                    });
                } else {
                    // 注意{}, json对象要加双引号，如："regcode": _this.company.regcode
                    $.post('/Enterprise/AddEnterprise', {
                        "regcode": _this.company.regcode,
                        "epname": _this.company.epname,
                        "address": _this.company.address,
                        "runscope": _this.company.runscope,
                        "mobiletele": _this.company.mobiletele,
                        "cellphone": _this.company.cellphone,
                        "regtime": _this.company.regtime,
                        "legalperson": _this.company.legalperson,
                        "intruduction": _this.company.editorHtml
                    }, function (data) {
                        alert(data.msg);
                        if( data.status == 'success' ) {
                            _this.$router.go("/index/allCompany/");
                        }
                    });
                }
            }
        },
        components: {
            'topnav': require('../components/topnav.vue'),
            'editor': require('../components/wangeditor/wangeditor.vue')
        },
        // 在创建实例时 `events` 选项简单地调用 `$on`
        events: {
            'editor-dispatch': function (html) {
                // 事件回调内的 `this` 自动绑定到注册它的实例上
                this.company.editorHtml = html;
            }
        }
    }
</script>

<style>
    .addCompany-body {

    }

    .addCompany-body .addCompany-content {
        background-color: #FFFFFF;
        border: 1px #1e88e5 solid;
        min-height: 300px;
        border-radius: 5px;
    }

    .addCompany-body .form-inline {
        display: inline-block;
        width: 250px;
        margin-left: 15px;
        margin-top: 15px;
        margin-bottom: 15px;
        overflow: hidden;
    }

    .addCompany-body .form-group {
        margin-top: 10px;
    }

    .addCompany-body .img-rounded {
        height: 50px;
        width: auto;
        cursor: pointer;
    }

    .addCompany-body .form-inline input {
        width: 250px;
    }

    .addCompany-body .form-editor {
        display: inline-block;
        height: 609px;
        margin-left: 15px;
        margin-top: 15px;
        margin-bottom: 15px;
    }

    .addCompany-body .form-submit {
        margin-left: 15px;
        margin-right: 15px;
        margin-top: 15px;
        margin-bottom: 15px;
    }
</style>
